package com.efounder.addressselector;

import com.efounder.addressselector.greendao.CityDao;
import com.efounder.addressselector.greendao.CountyDao;
import com.efounder.addressselector.greendao.SessionUtil;
import com.efounder.addressselector.greendao.StreetDao;
import com.efounder.addressselector.model.City;
import com.efounder.addressselector.model.County;
import com.efounder.addressselector.model.Province;
import com.efounder.addressselector.model.Street;

import org.greenrobot.greendao.query.Query;

import java.util.List;

public class DefaultAddressProvider implements AddressProvider {

    @Override
    public void provideProvinces(final AddressReceiver<Province> addressReceiver) {
        List<Province> provinceList = SessionUtil.getInstance().getProvinceDao().loadAll();
        addressReceiver.send(provinceList);
    }

    @Override
    public void provideCitiesWith(int provinceId, final AddressReceiver<City> addressReceiver) {
        Query<City> query = SessionUtil.getInstance().getCityDao().queryBuilder()
                .where(CityDao.Properties.Province_id.eq(provinceId)).build();
        addressReceiver.send(query.list());
    }

    @Override
    public void provideCountiesWith(int cityId, final AddressReceiver<County> addressReceiver) {
        Query<County> query = SessionUtil.getInstance().getCountyDao().queryBuilder()
                .where(CountyDao.Properties.City_id.eq(cityId)).build();
        addressReceiver.send(query.list());
    }

    @Override
    public void provideStreetsWith(int countyId, final AddressReceiver<Street> addressReceiver) {
        Query<Street> query = SessionUtil.getInstance().getStreetDao().queryBuilder()
                .where(StreetDao.Properties.County_id.eq(countyId)).build();
        addressReceiver.send(query.list());
    }
}
